var Class = { // 全局静态类, 用于声明一个新的类并提供构造函数支持

    create: function() {   
   
       return function() { // 返回一个函数, 代表着这个新声明的类的构造函数
   
         // 一个命名为initialize的函数将被这个类实现作为类的构造函数
   
         this.initialize.apply(this, arguments);// initialize函数将在你实例化一个变量的时候被调用执行(即上面7个步骤中的第5步)
   
       }
   
    }
   
   }
   var Person = Class.create();
   Person.prototype={
    
    initialize:function(name,x,y,w,h,headersrc,bodysrc,footersrc){
        this.name=name;
        this.srcx = x;
        this.srcy = y;
        this.x=x;
        this.y=y;
        this.w=w;
        this.h=h;
        this.headersrc=headersrc;
        this.bodysrc=bodysrc;
        this.footersrc=footersrc;
        this.headerimg = new Image();
        this.headerimg.src=this.headersrc;
        this.bodyimg = new Image();
        this.bodyimg.src=this.bodysrc;
        this.bodyimgsrc = this.bodysrc;

        this.footerimg = new Image();
        this.footerimg.src=this.footersrc;
        this.footerimgsrc = this.footersrc;

        this.anistate=0;//0 idle 1 attack 2 died
        if(this.name == "player"){
            // this.attack = parseInt(getKvFromCookie("attack"));
            this.attack = 50;
            this.defence = parseInt(getKvFromCookie("defence"));
            this.hpmax = parseInt(getKvFromCookie("hpmax"));
            this.hp = parseInt(getKvFromCookie("hp"));
            this.speed = parseInt(getKvFromCookie("speed"));
        }else if(this.name == "enemy"){
            this.attack = 6;
            this.defence = 4;
            this.hpmax =100;
            this.hp = 100;
            this.speed = 40;
        }
        this.bodyy=this.y+this.h;
        this.bodyx = this.x - 5;
        this.bodyw = this.w;
        this.bodyh = 15;

        this.footerx = this.x;
        this.footery = this.y+this.h+this.bodyh/2;
        this.footerw = 19;
        this.footerh = 10;
    },
    anim:function(tc,dstx){
        if(this.anistate == 0){
            
            this.x = this.srcx;
            this.y = this.srcy;
            this.bodyimg.src=this.bodyimgsrc;
                this.footerimg.src=this.footerimgsrc;
                this.bodyx = this.srcx-5;
                this.bodyw = this.w;
                this.footerx = this.srcx;
                this.bodyy=this.srcy+this.h;
                this.bodyh = 15;
        
                this.footery = this.srcy+this.h+this.bodyh/2;
                this.footerw = 19;
                this.footerh = 10;
            if(tc%10 == 0){
                this.bodyy = this.bodyy +1;
            }
            if(tc%10 == 2){
                this.bodyy = this.bodyy -1;
    
            }
        }else if(this.anistate == 1){
            if(this.name == "player"){
                if(tc%60<30){
                    this.x+=tc%30*40;
                    this.bodyimg.src="player_body_attack.png";
                    this.footerimg.src="footer_attack1.png";
                    this.bodyx = this.x;
                    this.bodyy=this.y+this.h;

                    this.bodyw = this.w *2;
                    this.footerx = this.x;
                }
         
               if(this.x > dstx){
                this.x = this.srcx;
                timecouts=0;
               this.anistate = 0;

               if(enemy.hp<=0)
            $("#attackBtn").attr("disabled",false);
                //enemy
                enemy.anistate=1;
               }
            }else if(this.name == "enemy"){
                if(tc%60<30){
                    this.x+=tc%30*40;
                    this.bodyimg.src="enemy_body_attack.png";
                    this.footerimg.src="footer1.png";
                    this.bodyx = this.x;
                    this.bodyy=this.y+this.h;
                    this.bodyw = this.w *2;
                    this.footerx = this.x;
                }
                if(this.x > dstx){
                    shake("canvas");
                    this.x = this.srcx;
                    timecouts=0;
                   this.anistate = 0;
                $("#attackBtn").attr("disabled",false);
                var mhp = $("#mhp");
                var mhpv = mhp.attr("aria-valuenow");
                var realAtk = enemy.attack-player.defence;
                console.log(mhpv)
                var newhp = parseInt(mhpv)-realAtk;
                if(newhp> 0){
                    setProgBarById("mhp",newhp,"我:"+newhp)

                }else{
                    player.anistate = 2;

                    setProgBarById("mhp",0,"我:"+newhp)
                    
                    console.log("失败")
                }
                }
            }
        }else if(this.anistate == 2){
            this.headerimg.src = "dead.png";
        }
      
    },
    moveTo:function(x,y){
            this.x+=(x-this.x)>=0?1:-1;
            this.y+=(y-this.y)>=0?1:-1;
            this.bodyx = this.x;
            this.footerx = this.x;
    },
    show:function(ctx){
        if(this.name == "player"){
            if(this.anistate == 2){
                ctx.drawImage(this.headerimg,this.x,this.y,this.w,this.h);

            }else{
                var msx =  ctx.canvas.width -mouse.x;
                var msy = mouse.y-window.canvas.getBoundingClientRect().y;
                if(enemy.anistate == 2){
                    
                     this.bodyx = this.x-5;
                    this.footerx = this.x;
                     this.bodyy=this.y+this.h;
            
                     this.footery = this.y+this.h+this.bodyh/2;
                }
                ctx.drawImage(this.headerimg,this.x,this.y,this.w,this.h);
                ctx.drawImage(this.bodyimg,this.bodyx,this.bodyy,this.bodyw,this.bodyh);
                ctx.drawImage(this.footerimg,this.footerx,this.footery,this.footerw,this.footerh);
            }
           
        }else if(this.name == "enemy"){
            if(this.anistate == 2){
            ctx.drawImage(this.headerimg,this.x-3,this.y,this.w,this.h);
            // var baowuImg = new Image();
            // baowuImg.src = "yinhun.png";
            // ctx.drawImage(baowuImg,this.x+20,this.y+20,24,18);

            // var itemFlashImg = new Image();
            // if(timecouts%2==0){
            //     itemFlashImg.src = "item_flash.png";

            // }else{
            // itemFlashImg.src = "item_flash2.png";

            // }
            // ctx.drawImage(itemFlashImg,this.x+10,this.y+10,38,38);
            }else{
            ctx.drawImage(this.headerimg,this.x-3,this.y,this.w,this.h);

                ctx.drawImage(this.bodyimg,this.bodyx,this.bodyy,this.bodyw,this.bodyh);
                ctx.drawImage(this.footerimg,this.footerx,this.footery,this.footerw,this.footerh);
            }
         
        }
       
    },
    attack:function(ctx, src, dest){

    },
    printName:function(){
        alert(this.name)
    }
   }